﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.Controller;
using VideoRentalShop.View.Templates;
using VideoRentalShop.Model;

namespace VideoRentalShop.View.Panel.RentStepPanel
{
    /// <summary>
    /// Logika interakcji dla klasy RentSummaryStepPanel.xaml
    /// </summary>
    public partial class RentSummaryStepPanel : StepPanelContent
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public RentSummaryStepPanel()
        {
            InitializeComponent();
        }


        public override void RefreshForm()
        {
            var item = _item as ClientOrder;

            if (item == null)
            {
                ClearForm();
                return;
            }

            var clientId = item.ClientId;
            var movies = item.Movies;
            var date = item.Date = DateTime.Now;
            lblDate.Content = date.ToString(VideoRentalShop.Properties.Resources.StrDateFormat);

            var client = _controller.Data.Client.Get(clientId);
            if (client != null)
            {
                lblClient.Content = string.Format(string.Format("{0} {1}", client.LastName, client.Name));
            }

            lstMovies.Items.Clear();
            foreach (var movie in movies)
            {
                var id = movie.MovieId;
                var time = movie.Time;

                var m = _controller.Data.Movie.GetAll().Find(id);

                if (m != null)
                {
                    lstMovies.Items.Add(new
                    {
                        Title = m.Title,
                        DateTo = string.Format("{0} ({1}h)", date.AddHours(time).ToString(VideoRentalShop.Properties.Resources.StrDateFormat), time)
                    });
                }
            }
        }

        public override void ClearForm()
        {
            lblClient.Content = string.Empty;
            lblDate.Content = string.Empty;
            lstMovies.Items.Clear();
        }

        public override bool ValidateForm()
        {
            return AddOrder(_item as ClientOrder);
        }

        private bool AddOrder(ClientOrder itemData)
        {
            var order = new Order();
            order.MakeDate = itemData.Date;
            order.ClientId = itemData.ClientId;
            order.Archived = false;

            foreach (var item in itemData.Movies)
            {
                var orderDetails = new OrderDetails();
                var movie = _controller.Data.Movie.GetAll().Find(item.MovieId);

                if (movie != null)
                {
                    movie.Avaiable--;
                    orderDetails.Movie = movie;
                }
                else
                {
                    orderDetails.MovieId = item.MovieId;
                    System.Diagnostics.Debug.WriteLine("Oj niedobrze! (RentSummaryStepPanel.AddOrder() // line: ok. 114");
                }
                orderDetails.Order = order;
                orderDetails.MaxReturnDate = itemData.Date.AddHours(item.Time);

                order.OrderDetails.Add(orderDetails);
            }

            var valid = _controller.Data.Order.IsValid(order);

            if (valid)
            {
                valid &= _controller.Data.Order.Add(order);
            }

            if (!valid)
            {
                _controller.ShowMessage("Podczas dodawania zamówienia wystąpił błąd.", "Błąd", MessageType.Error);
            }

            return valid;
        }
    }
}
